#include <stdio.h>
#include <stdlib.h>
#include <evhttp.h>
#include <event.h>
#include <string.h>

extern "C"{
#include "event2/http.h"
#include "event2/event.h"
#include "event2/buffer.h"
#include "event2/bufferevent.h"
#include "event2/bufferevent_compat.h"
#include "event2/http_struct.h"
#include "event2/http_compat.h"
#include "event2/util.h"
#include "event2/listener.h"
}

void Test_Get(struct evhttp_request *req,void *arg)
{
{
//解析头，进行处理
//这里只做了回复
}
struct evbuffer* retbuff = evbuffer_new();
if(NULL == retbuff)
{
printf("retbuff is NULL\n");
return ;
}
evbuffer_add_printf(retbuff,"Test_Get is OK!");
evhttp_send_reply(req,HTTP_OK,"Client is OK",retbuff);
evbuffer_free(retbuff);
}
void Test_Post(struct evhttp_request *req,void *arg)
{
{
//获取数据，然后进行处理
//evbuffer_pullup()获取数据接口
}
//只做回复
struct evbuffer* retbuff = evbuffer_new();
if(NULL == retbuff)
{
printf("retbuff is NULL\n");
return ;
}
evbuffer_add_printf(retbuff,"Test_post is OK!");
evhttp_send_reply(req,HTTP_OK,"Client is OK",retbuff);
evbuffer_free(retbuff);
}

int main(int argc, char **argv)
{
    // short http_port = 8081;
    // char *http_addr = "127.0.0.1";

	if (argc < 2)
	{
		printf("please input IP and port\n");
		return -1;
	}
	
	short http_port = atoi(argv[2]);
    char *http_addr = argv[1];

    //初始化
    event_init();
    //启动http服务端
    struct evhttp *http_server = evhttp_start(http_addr,http_port);
    if(http_server == NULL)
    {
        printf("====line:%d,%s\n",__LINE__,"http server start failed.");
        return -1;
    }

    //设置请求超时时间(s)
    evhttp_set_timeout(http_server,5);
    //设置事件处理函数，evhttp_set_cb针对每一个事件(请求)注册一个处理函数，
    //区别于evhttp_set_gencb函数，是对所有请求设置一个统一的处理函数
    evhttp_set_cb(http_server,"/hello/post",Test_Post,NULL);
    evhttp_set_cb(http_server,"/hello/get",Test_Get,NULL);

    //循环监听
    event_dispatch();
    evhttp_free(http_server);

    return 0;
}

